<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单结算类
 * @package  module/base/bill
 * @author   allen.qiang
 * @version 1.0
 */

class billCheckout {

    /**
     * 查询会员全部发票信息
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买记录
     * @return array|null
     */
    public function getInvoiceList( &$bill_master_array, &$cust_buy_log ) {

        if (!isset($cust_buy_log['pcustID']) || empty($cust_buy_log['pcustID'])) {
            return null;
        }

        //加载会员发票类
        quanli_require('module/base/customer/custInvoice.class.php');

        //实例会员发票类
        $custInvoice = new custInvoice();

        //查询发票信息
        $arg_array = array(
            'pcustID' => $cust_buy_log['pcustID']
        );
        $invoice_array = $custInvoice->searchCustInvoice( $arg_array );

        if ($invoice_array) {
            foreach($invoice_array as &$invoice) {
                if ($invoice['invoiceID'] == $cust_buy_log['invoiceID']) {
                    $bill_master_array['invoice'] = 1;
                    $bill_master_array['invoiceID'] = $invoice['invoiceID'];
                    $bill_master_array['invoice_type'] = $invoice['invoice_type'];
                    $bill_master_array['invoice_title_type'] = $invoice['invoice_title_type'];
                    $bill_master_array['invoice_title'] = $invoice['invoice_title'];
                    $cust_buy_log['invoiceID'] = $bill_master_array['invoiceID'];
                }
            }
        }

        return $invoice_array;

    }

    /**
     * 查询会员收货地址列表
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @return null|array
     */
    public function getDeliverList( &$bill_master_array, &$cust_buy_log ) {

        //加载会员地址类
        quanli_require('module/base/customer/custDeliver.class.php');

        //实例会员地址类
        $custDeliver = new custDeliver();

        $arg_array = array(
            'pcustID' => $cust_buy_log['pcustID'],
            'cityid' => $bill_master_array['cityid'],
            'sortname' => 'deliverID',
            'sortorder' => 'DESC'
        );

        //查询会员收货地址
        $deliver_array = $custDeliver->searchCustDeliver($arg_array);
        if (!$deliver_array)
            return null;

        $deliverFlg = 0;
        foreach($deliver_array as $deliver) {
            if ($deliverFlg == 0 || $deliver['deliverID'] == $cust_buy_log['deliverID']) {
                ++$deliverFlg;
                $bill_master_array['deliverid'] = $deliver['deliverID'];
                $bill_master_array['deliver_province'] = $deliver['provinceName'];
                $bill_master_array['deliver_city'] = $deliver['cityName'];
                $bill_master_array['deliver_county'] = $deliver['countyName'];
                $bill_master_array['deliver_address'] = $deliver['address'];
                $bill_master_array['deliver_name'] = $deliver['custName'];
                $bill_master_array['deliver_postalCode'] = $deliver['postalCode'];
                $bill_master_array['deliver_mobile'] = $deliver['mobile'];
                $bill_master_array['deliver_phone'] = $deliver['phone'];
                $bill_master_array['deliver_type'] = $deliver['type'];
                $bill_master_array['countyid'] = $deliver['countyid'];
                $bill_master_array['cityid'] = $deliver['cityid'];
                $bill_master_array['provinceid'] = $deliver['provinceid'];
                $bill_master_array['deliver_type'] = $deliver['type'];      //收货地址类型： 1、收货地址 2、自提类型
                $bill_master_array['distribution_id'] = $deliver['distribution_id'];

                /**
                 * 地址经纬度为空
                 */
                if (empty($deliver['longitude']) || empty($deliver['latitude'])) {

                    //mod allen.qiang 20141210 FUN-BK-0041-G7地址库平台接入---start
                    $address_detail= $deliver['provinceName'] . $deliver['cityName'] . $deliver['countyName'] . $deliver['address'];
                    if(G_LATITUDE_COUNTY==0){ //地址经纬度不开启区匹配
                        $address_detail =$deliver['provinceName'] . $deliver['cityName'] . $deliver['address'];
                    }
                    $coordinate_array = get_address_coordinate( $address_detail );
                    //$coordinate_array = get_address_coordinate( $deliver['provinceName'] . $deliver['cityName'] . $deliver['countyName'] . $deliver['address'] );
                    //mod allen.qiang 20141210 FUN-BK-0041-G7地址库平台接入---end

                    if ($coordinate_array['match'] == 1) {
                        $result_array = $custDeliver->updateAddressCoordinate( $deliver['deliverID'], $coordinate_array );
                        if ($result_array['code'] == 200) {
                            $deliver['longitude'] = $coordinate_array['longitude'];
                            $deliver['latitude'] = $coordinate_array['latitude'];
                            $deliver['precise'] = $coordinate_array['precise'];
                        }
                    }
                }

                $bill_master_array['longitude'] = $deliver['longitude'];
                $bill_master_array['latitude'] = $deliver['latitude'];
                $bill_master_array['precise'] = $deliver['precise'];
            }
        }

        //查询会员最早一次添加的地址--当订单是试吃订单的时候 add by loreta.cheng 2015-06-09  start
        if(isset($bill_master_array['billType']) && $bill_master_array['billType']==9){
            $pcustOldAdrress = $custDeliver->getBoldAddress( $cust_buy_log['pcustID'] );
            if($pcustOldAdrress['deliverID']){
               $cust_buy_log['deliverID'] = $pcustOldAdrress['deliverID'];
            }else{
               $cust_buy_log['deliverID'] = $bill_master_array['deliverid'];
            }
        }else{
            $cust_buy_log['deliverID'] = $bill_master_array['deliverid'];
        }
        //查询会员最早一次添加的地址--当订单是试吃订单的时候add by loreta.cheng 2015-06-09  end
        return $deliver_array;

    }

    /**
     * 根据收货地址查询配送方式
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @return array|null
     */
    public function getSendTypeList( &$bill_master_array, &$cust_buy_log = null ) {

        quanli_require('module/base/system/sysSend.class.php');

        //实例化配送方式类
        $sysSend = new sysSend();

        if ($bill_master_array['deliver_type'] == 2) {

            //查询自提类型的配送方式
            $arg_array = array(
                'is_take_their' => 1
            );

            $sendType_array = $sysSend->searchSendType( $arg_array );
            $sendType_array = $sendType_array[0];

            $return_array[$sendType_array['sendTypeID']] = array(
                'sendTypeID' => $sendType_array['sendTypeID'],
                'sendTypeName' => $sendType_array['sendTypeName'],
                'send_region_id' => null,
                'send_region_name' => null,
                'description' => $sendType_array['description'],
                'is_match' => 1
            );

            $bill_master_array['sendTypeID'] = $sendType_array['sendTypeID'];
            $bill_master_array['sendTypeName'] = $sendType_array['sendTypeName'];
            $bill_master_array['deliver_fee'] = 0;
            $bill_master_array['deliver_feePaid'] = 0;
            $bill_master_array['send_region_id'] = null;
            $bill_master_array['send_region_name'] = null;
            $bill_master_array['is_match'] = 1;
            $bill_master_array['extend_1'] = null;
            $bill_master_array['extend_9'] = null;
            $bill_master_array['extend_10'] = null;
            $bill_master_array['extend_2'] = null;
            $bill_master_array['extend_11'] = null;

        } else {

            $is_match = 1;

            //得到地图区域
            $result_array = $this->getMapArea( $bill_master_array['longitude'], $bill_master_array['latitude'], $bill_master_array['cityid'], 2);
            $areaID = $result_array['areaID'];
            if ($areaID == 0) {
                $is_match = 0;
                $areaID = $result_array['default_areaID'];
            }

            //查询地图区域下的所有配送区域
            $arg_array = array(
                'areaID' => $areaID
            );
            $send_area_array = $sysSend->searchSendArea( $arg_array );
            if (!$send_area_array) {
                return null;
            }

            $return_array = array();

            $sendFlg = 0;
            $freight = 0;
            foreach($send_area_array as &$send) {

                //根据配送区域查询配送方式
                $arg_array = array(
                    'sendTypeID' => $send['sendTypeID']
                );

                $sendType_array = $sysSend->searchSendType( $arg_array );
                if(!$sendType_array)
                    continue;
                $sendType_array = $sendType_array[0];

                $return_array[$send['sendTypeID']] = array(
                    'sendTypeID' => $sendType_array['sendTypeID'],
                    'sendTypeName' => $sendType_array['sendTypeName'],
                    'freight' => $send['freight'],
                    //add 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
                    'overweight_freight' => $send['overweight_freight'],
                    //add 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
                    'send_region_id' => $send['send_region_id'],
                    'send_region_name' => $send['send_region_name'],
                    'description' => $sendType_array['description'],
                    'is_match' => $is_match
                );

                if ($sendFlg == 0 || (!is_null($cust_buy_log) && $send['send_region_id'] == $cust_buy_log['send_region_id']) || $freight > $send['freight']) {
                    ++$sendFlg;
                    $freight = $send['freight'];
                    $bill_master_array['sendTypeID'] = $sendType_array['sendTypeID'];
                    $bill_master_array['sendTypeName'] = $sendType_array['sendTypeName'];
                    //mod 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
                    $bill_master_array['deliver_fee'] = $send['overweight_freight'];
                    //$bill_master_array['deliver_fee'] = $send['freight'];
                    //mod 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
                    $bill_master_array['deliver_feePaid'] = $send['freight'];
                    $bill_master_array['send_region_id'] = $send['send_region_id'];
                    $bill_master_array['send_region_name'] = $send['send_region_name'];
                    $bill_master_array['is_match'] = $is_match;
                    $bill_master_array['extend_1'] = null;
                    $bill_master_array['extend_9'] = null;
                    $bill_master_array['extend_10'] = null;
                    $bill_master_array['extend_2'] = null;
                    $bill_master_array['extend_11'] = null;
                }

            }

        }



        if(empty($return_array)){
            return null;
        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['send_region_id'] = $bill_master_array['send_region_id'];
        }

        return $return_array;

    }

    /**
     * 获得地图区域
     * @param double $longitude 经度
     * @param double $latitude 纬度
     * @param int $cityID 城市ID
     * @param int $type 1、配送站 2、运费
     * @return null|array
     */
    public function getMapArea($longitude, $latitude, $cityID, $type , $brandID = G_BRANDID_DEFAULT) {

        quanli_require('module/base/system/mapAreaZoning.class.php');
        quanli_require('module/base/common/quanliPointLocation.class.php');

        //实例地图区域类
        $mapAreaZoning = new mapAreaZoning( );

        $quanliPointLocation = new quanliPointLocation();


        //查询城市下的所有地图区域
        $arg_array = array(
            'cityID' => $cityID,
            'type' => $type,
            'useFlg' => 1,
            'brandID' => $brandID
        );
        $map_area_array = $mapAreaZoning->searchMapArea( $arg_array );
        if(!$map_area_array){
            return null;
        }

        $result_array = array(
            'areaID' => 0,
            'default_areaID' => 0
        );

        //城市经纬度
        $lgtLat = $longitude . ',' . $latitude;

        foreach($map_area_array as $area) {

            if (empty($result_array['default_areaID'])) {
                $result_array['default_areaID'] = $area['areaID'];
            }

            //经纬度集合
            $lgtLat_array = array();
            $map_area_detail_array = $mapAreaZoning->searchMapAreaPath( $area['areaID'] );
            foreach($map_area_detail_array as $area_detail){
                $lgtLat_array[] = $area_detail['longitude'].','.$area_detail['latitude'];
            }



            //通函数筛选出匹配的经纬度区域  匹配到返回区域ID  否则返回false
            $filter_result = $quanliPointLocation->polygon($lgtLat, $lgtLat_array);
            if(!$filter_result){
                continue;
            }

            if($result_array['areaID'] == 0){
                $result_array['areaID'] = $area['areaID'];
                break;
            }

        }
        return $result_array;

    }

    /**
     * 根据收货地址查询配送站和配送员
     * @param array $bill_master_array 订单数组
     * @return array|null
     */
    public function getDistributionList( &$bill_master_array ) {

        quanli_require('module/base/system/sysDistribution.class.php');

        //实例化配送站设置类
        $sysDistribution = new sysDistribution( );

        if ($bill_master_array['deliver_type'] == 1) {

            $bill_master_array['distribution_id'] = 0;
            $bill_master_array['distribution_name'] = null;

            //得到地图区域
            $result_array = $this->getMapArea( $bill_master_array['longitude'], $bill_master_array['latitude'], $bill_master_array['cityid'], 1);
            $areaID = $result_array['areaID'];

            if ($areaID == 0) {

                //查询配送站
                $arg_array = array(
                    'cityid' => $bill_master_array['cityid'],
                    'is_default' => 1
                );

                $distribution_array = $sysDistribution->search( $arg_array );
                if($distribution_array)
                {
                    $distribution_id = $distribution_array[0]['distribution_id'];
                }

            } else {

                //查询地图区域和配送站的映射关系
                $arg_array = array(
                    'areaID' => $areaID
                );
                $distribution_map_area = $sysDistribution->searchDistributionMapArea( $arg_array );
                if(!$distribution_map_area) {
                    return null;
                }

                $distribution_id = $distribution_map_area[0]['distribution_id'];

            }



        } else {

            $distribution_id = $bill_master_array['distribution_id'];

        }


        if (!isset($distribution_array)) {

            //查询配送站
            $arg_array = array(
                'distribution_id' => $distribution_id,
                'useFlg' => 1
            );

            $distribution_array = $sysDistribution->search( $arg_array );

            if(!$distribution_array) {
                return null;
            }

        }

        $bill_master_array['distribution_id'] = $distribution_array[0]['distribution_id'];
        $bill_master_array['distribution_name'] = $distribution_array[0]['distribution_name'];

        return array(
            'distribution_id' => $bill_master_array['distribution_id'],
            'distribution_name' => $bill_master_array['distribution_name']
        );

    }


    /**
     * 根据配送方式，查询支付方式
     * @param string $source 来源
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @param str $extend1 查询条件 add by allen.qiang
     * @param str $extend2 查询条件 add by allen.qiang
     * @return array|null
     */
    public function getPayTypeList( $source, &$bill_master_array, &$cust_buy_log=null,$extend1='',$extend2='') {

        //加载支付方式类
        quanli_require('module/base/system/sysPayment.class.php');

        //实例支付方式类
        $sysPayment = new sysPayment();

        //查询支付类型
        $arg_array = array(
            'sendTypeID' => $bill_master_array['sendTypeID'],
            'sortname' => 'pay_type',
            'sortorder' => 'ASC'
        );
        $pay_array = $sysPayment->searchPayTypeFromSendType( $arg_array );
        if (!$pay_array)
            return null;

        $item_array = array();

        foreach($pay_array as &$item) {

            //根据支付类型查询支付方式
            $arg_array = array (
                'source' => $source,
                'type' => $item['pay_type'],
                'sortname' => 'payTypeName',
                'sortorder' => 'ASC',
                'useFlg' => 1
            );

            //扩展条件 add by allen.qiang 2015-05-03
            $arg_array['extend'] = !empty($extend1)?$extend1:'';

            $pay_type_array = $sysPayment->searchPayType( $arg_array );

            if (!$pay_type_array)
                continue;

            //如果是货到付款，需查询所有付款方式
            foreach($pay_type_array as $key => &$pay) {

                if (isset($cust_buy_log['payTypeID']) && $pay['payTypeID'] == $cust_buy_log['payTypeID']) {

                    $bill_master_array['pay_type'] = $pay['type'];
                    $bill_master_array['payTypeID'] = $pay['payTypeID'];
                    $bill_master_array['payTypeName'] = $pay['payTypeName'];
                    $cust_buy_log['pay_type'] = $bill_master_array['pay_type'];
                    $cust_buy_log['payTypeID'] = $bill_master_array['payTypeID'];
                }

                if($pay['type'] == 1){

                    $arg_array = array(
                        'payTypeID' => $pay['payTypeID'],
                        'sortname' => 'name',
                        'sortorder' => 'ASC'
                    );

                    //扩展条件 add by allen.qiang 2015-05-03
                    $arg_array['extend'] = !empty($extend2)?$extend2:'';

                    $pay['list'] =  $sysPayment->searchPaymentMethod($arg_array);

                    if ($pay['list']) {
                        foreach($pay['list'] as &$payment) {
                            if ($payment['id'] == $cust_buy_log['payment_method_id']) {

                                $bill_master_array['payment_method_id'] = $payment['id'];
                                $bill_master_array['payment_method_name'] = $payment['name'];
                                $bill_master_array['payment_method_type'] = $payment['type'];
                                $bill_master_array['card_is_invoice'] = $payment['is_invoice'];
                                $cust_buy_log['payment_method_type'] = $bill_master_array['payment_method_type'];
                                $cust_buy_log['payment_method_id'] = $bill_master_array['payment_method_id'];
                                $cust_buy_log['is_invoice'] = $bill_master_array['card_is_invoice'];
                            }
                        }
                    }

                }
            }

            $item_array[] = array(
                'sendTypeID' => $item['sendTypeID'],
                'type' => $item['pay_type'],
                'list' => $pay_type_array
            );

        }

        return $item_array;

    }

    /**
     * 根据支付方式，查询付款方式
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @return array|null
     */
    public function getPaymentMethodList( &$bill_master_array, &$cust_buy_log=null ) {

        //加载支付方式类
        quanli_require('module/base/system/sysPayment.class.php');

        //实例支付方式类
        $sysPayment = new sysPayment();

        if(!isset($bill_master_array['payTypeID']) || empty($bill_master_array['payTypeID'])){
            return null;
        }

        //根据支付类型查询支付方式
        $arg_array = array(
            'payTypeID' => $bill_master_array['payTypeID'],
            'type' => 1     //货到付款类型
        );
        $pay_type_array = $sysPayment->searchPayType( $arg_array );

        if (!$pay_type_array){

            $bill_master_array['payment_method_id'] = 0;
            $bill_master_array['payment_method_name'] = null;
            $bill_master_array['payment_method_type'] = null;
            return null;
        }

        foreach($pay_type_array as &$pay_type) {

            $payment_method_array =  $sysPayment->searchPaymentMethod($arg_array);

            if(!$payment_method_array){
                continue;
            }

            foreach($payment_method_array as $payment){
                if ((!is_null($cust_buy_log) && $pay_type['payTypeID'] == $cust_buy_log['payTypeID'])) {
                    $bill_master_array['payment_method_id'] = $payment['id'];
                    $bill_master_array['payment_method_name'] = $payment['name'];
                    $bill_master_array['payment_method_type'] = $payment['type'];
                }
            }

            $item_array[] = array(
                'payTypeID' => $pay_type['payTypeID'],
                'type' => $pay_type['type'],
                'list' => $payment_method_array
            );

        }

        if(empty($item_array)){
            return null;
        }

        if (!is_null($cust_buy_log)) {
            $cust_buy_log['payment_method_id'] = $bill_master_array['payment_method_id'];
        }

        return $item_array;

    }

    /**
     * 查询会员地址详情
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @param int $deliverID 选中的收货地址ID
     * @return bool
     */
    public function getDeliver( &$bill_master_array, &$cust_buy_log, $deliverID ) {

        //加载会员地址类
        quanli_require('module/base/customer/custDeliver.class.php');

        //实例会员地址类
        $custDeliver = new custDeliver();

        //查询会员收货地址
        $arg_array = array(
            'pcustID' => $cust_buy_log['pcustID'],
            'cityid' => $bill_master_array['cityid'],
            'deliverID' => $deliverID
        );
        $deliver_array = $custDeliver->searchCustDeliver($arg_array);
        if (!$deliver_array)
            return false;
        $deliver = $deliver_array[0];

        $bill_master_array['deliverid'] = $deliverID;
        $bill_master_array['deliver_province'] = $deliver['provinceName'];
        $bill_master_array['deliver_city'] = $deliver['cityName'];
        $bill_master_array['deliver_county'] = $deliver['countyName'];
        $bill_master_array['deliver_address'] = $deliver['address'];
        $bill_master_array['deliver_name'] = $deliver['custName'];
        $bill_master_array['deliver_mobile'] = $deliver['mobile'];
        $bill_master_array['deliver_phone'] = $deliver['phone'];
        $bill_master_array['countyid'] = $deliver['countyid'];
        $bill_master_array['cityid'] = $deliver['cityid'];
        $bill_master_array['provinceid'] = $deliver['provinceid'];

        /**
         * 地址经纬度为空
         */
        if (empty($deliver['longitude']) || empty($deliver['latitude'])) {

            //mod allen.qiang 20141210 FUN-BK-0041-G7地址库平台接入---start
            $address_detail= $deliver['provinceName'] . $deliver['cityName'] . $deliver['countyName'] . $deliver['address'];
            if(G_LATITUDE_COUNTY==0){ //地址经纬度不开启区匹配
                $address_detail =$deliver['provinceName'] . $deliver['cityName'] . $deliver['address'];
            }
            $coordinate_array = get_address_coordinate( $address_detail );
            //$coordinate_array = get_address_coordinate( $deliver['provinceName'] . $deliver['cityName'] . $deliver['countyName'] . $deliver['address'] );
            //mod allen.qiang 20141210 FUN-BK-0041-G7地址库平台接入---end

            if ($coordinate_array['match'] == 1) {
                $result_array = $custDeliver->updateAddressCoordinate( $deliver['deliverID'], $coordinate_array );
                if ($result_array['code'] == 200) {
                    $deliver['longitude'] = $coordinate_array['longitude'];
                    $deliver['latitude'] = $coordinate_array['latitude'];
                    $deliver['precise'] = $coordinate_array['precise'];
                }
            }
        }

        $bill_master_array['longitude'] = $deliver['longitude'];
        $bill_master_array['latitude'] = $deliver['latitude'];
        $bill_master_array['precise'] = $deliver['precise'];
        $bill_master_array['deliver_type'] = $deliver['type'];
        $bill_master_array['distribution_id'] = $deliver['distribution_id'];

        $cust_buy_log['deliverID'] = $deliverID;

        return true;

    }

    /**
     * 根据收货地址查询配送方式
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买的记录
     * @param int $send_region_id 选中的配送区域ID
     * @return array|null
     */
    public function getSendType( &$bill_master_array, &$cust_buy_log, $send_region_id ) {

        quanli_require('module/base/system/sysSend.class.php');

        //实例化配送方式类
        $sysSend = new sysSend( );


        //查询地图区域下的所有配送区域
        $arg_array = array(
            'send_region_id' => $send_region_id
        );
        $send_area_array = $sysSend->searchSendArea( $arg_array );
        if(!$send_area_array){
            return null;
        }

        //根据配送区域查询配送方式
        $arg_array = array(
            'sendTypeID' => $send_area_array[0]['sendTypeID']
        );
        $sendType_array = $sysSend->searchSendType( $arg_array );
        if(!$sendType_array) {
            return null;
        }

        $bill_master_array['sendTypeID'] = $sendType_array[0]['sendTypeID'];
        $bill_master_array['sendTypeName'] = $sendType_array[0]['sendTypeName'];
        //mod 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
        $bill_master_array['deliver_fee'] = $send_area_array[0]['overweight_freight'];
        //$bill_master_array['deliver_fee'] = $send_area_array[0]['freight'];
        //mod 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
        $bill_master_array['deliver_feePaid'] = $send_area_array[0]['freight'];
        $bill_master_array['send_region_id'] = $send_area_array[0]['send_region_id'];
        $bill_master_array['send_region_name'] = $send_area_array[0]['send_region_name'];
        $bill_master_array['extend_1'] = null;
        $bill_master_array['extend_9'] = null;
        $bill_master_array['extend_10'] = null;
        $bill_master_array['extend_2'] = null;
        $bill_master_array['extend_11'] = null;

        $cust_buy_log['send_region_id'] = $send_area_array[0]->send_region_id;

        return true;

    }

    /**
     * 查询支付方式详情
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @param int $payTypeID 选中的支付方式ID
     * @return array|null
     */
    public function getPayType( &$bill_master_array, &$cust_buy_log, $payTypeID ) {

        //加载支付方式类
        quanli_require('module/base/system/sysPayment.class.php');

        //实例支付方式类
        $sysPayment = new sysPayment();

        //根据支付类型查询支付方式
        $arg_array = array(
            'payTypeID' => $payTypeID
        );
        $pay_type_array = $sysPayment->searchPayType( $arg_array );
        if (!$pay_type_array)
            return false;

        $bill_master_array['pay_type'] = $pay_type_array[0]['type'];
        $bill_master_array['payTypeID'] = $pay_type_array[0]['payTypeID'];
        $bill_master_array['payTypeName'] = $pay_type_array[0]['payTypeName'];

        $cust_buy_log['pay_type'] = $bill_master_array['pay_type'];
        $cust_buy_log['payTypeID'] = $bill_master_array['payTypeID'];

        return true;

    }

    /**
     * 查询付款方式详情
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买时的记录
     * @param int $payment_method_id 选中的支付方式ID
     * @return array|null
     */
    public function getPayment( &$bill_master_array, &$cust_buy_log, $payment_method_id ) {

        //加载支付方式类
        quanli_require('module/base/system/sysPayment.class.php');

        //实例支付方式类
        $sysPayment = new sysPayment();

        //根据支付类型查询支付方式
        $arg_array = array(
            'payTypeID' => $bill_master_array['payTypeID'],
            'id' => $payment_method_id
        );


        $payment_method_array = $sysPayment->searchPaymentMethod( $arg_array );
        if (!$payment_method_array)
            return false;

        $bill_master_array['payment_method_id'] = $payment_method_array[0]['id'];
        $bill_master_array['payment_method_name'] = $payment_method_array[0]['name'];
        $bill_master_array['payment_method_type'] = $payment_method_array[0]['type'];
        $bill_master_array['card_is_invoice'] = $payment_method_array[0]['is_invoice'];

        $cust_buy_log['payment_method_type'] = $bill_master_array['payment_method_type'];
        $cust_buy_log['payment_method_id'] = $bill_master_array['payment_method_id'];
        $cust_buy_log['is_invoice'] = $bill_master_array['card_is_invoice'];

        return true;

    }

    /**
     * 查询选中的发票信息
     * @param array $bill_master_array 订单数组
     * @param array $cust_buy_log 会员上次购买记录
     * @param int $invoiceID 选中的发票ID
     * @return bool
     */
    public function getInvoice( &$bill_master_array, &$cust_buy_log, $invoiceID ) {

        if ($invoiceID) {

            //加载会员发票类
            quanli_require('module/base/customer/custInvoice.class.php');

            //实例会员发票类
            $custInvoice = new custInvoice();

            //查询发票信息
            $arg_array = array(
                'pcustID' => $cust_buy_log['pcustID'],
                'invoiceID' => $invoiceID
            );
            $invoice_array = $custInvoice->searchCustInvoice( $arg_array );
            if (!$invoice_array)
                return false;
            $invoice = $invoice_array[0];

            $bill_master_array['invoice'] = 1;
            $bill_master_array['invoiceID'] = $invoice['invoiceID'];
            $bill_master_array['invoice_type'] = $invoice['invoice_type'];
            $bill_master_array['invoice_title_type'] = $invoice['invoice_title_type'];
            $bill_master_array['invoice_title'] = $invoice['invoice_title'];


        } else {

            $bill_master_array['invoice'] = 0;
            $bill_master_array['invoiceID'] = 0;
            $bill_master_array['invoice_type'] = null;
            $bill_master_array['invoice_title_type'] = null;
            $bill_master_array['invoice_title'] = null;

        }

        $cust_buy_log['invoiceID'] = $bill_master_array['invoiceID'];

        return true;

    }

    public function checkDistributionSendRestrict($send_date, $send_time, $distribution_id) {

        global $G_SHOP;

        $return_array = array(
            'h' => 0, //是否爆单 1、是 0、否
            's' => 1 //爆单后是否可继续配送 1、是 0、否
        );

        if (empty($distribution_id)) {
            return $return_array;
        }

        //查询配送站当前时间段运能限制
        $distribution_send_restrict_obj =& $G_SHOP->DBCA->stmt_query('SELECT * FROM sys_distribution_send WHERE distribution_id = ?
                                               AND send_start_time <= ? AND send_end_time >= ?', 'iss', $distribution_id, $send_time, $send_time);
        if ($distribution_send_restrict_obj) {

            //检查当前日期、当前时间段的配送站未取消的订单数量
//            $bill_count_obj =& $G_SHOP->DBCA->stmt_query('SELECT COUNT(*) AS bill_count FROM bill_master WHERE send_date = ? AND
//                                                          send_start_time = ? AND distribution_id = ? AND bill_status <> 10', 'ssi',
//                                                          $send_date, $send_time, $distribution_id);

            $bill_count_obj =& $G_SHOP->DBCA->stmt_query("SELECT COUNT(*) AS bill_count FROM bill_master,bill_goods WHERE send_date = ? AND
                                                          send_start_time = ? AND distribution_id = ? AND bill_status <> 10
                                                          AND bill_master.`bill_no` = bill_goods.`bill_no` AND bill_goods.product_code != 't0399'", 'ssi',
                $send_date, $send_time, $distribution_id);


            if ($bill_count_obj[0]->bill_count > 0) {

                if ($distribution_send_restrict_obj[0]->max_bill <= $bill_count_obj[0]->bill_count) {
                    $return_array['h'] = 1; //是否爆单 1、是 0、否
                    $return_array['s'] = $distribution_send_restrict_obj[0]->send_status; //爆单后是否可继续配送 1、是 0、否
                }
            }
        }
        return $return_array;
    }
}

